package com.hxkcloud.patient.receiver;

import android.app.ActivityManager;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.hxkcloud.patient.YjhApplication;
import com.hxkcloud.patient.activitys.DoctorFragmentActivity;
import com.hxkcloud.patient.activitys.MessageMainActivity;
import com.hxkcloud.patient.activitys.NurseFragmentActivity;
import com.hxkcloud.patient.activitys.UserFragmentActivity;
import com.hxkcloud.patient.alarm.Alarm;
import com.hxkcloud.patient.alarm.Alarms;
import com.hxkcloud.patient.beans.LoginInfo;
import com.hxkcloud.patient.beans.RemindTimeBean;
import com.hxkcloud.patient.dborm.LoginInfoDao;
import com.hxkcloud.patient.fragments.ConsulteFragment;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.utils.BadgeUtil;
import com.hxkcloud.patient.utils.SharedPreferenceUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.service.PushReceiver;

/**
 * Created by william_sun on 2016-09-21.
 * Description:
 */
public class JpushReceiver extends PushReceiver {
    private static final String TAG = "JpushReceiver";

    private NotificationManager nm = null;

    private YjhApplication application = null;
    private static String PUSH_TYPE_NEW_DOCTOR_REPLY = "10";

    private String packageName = "com.hxkcloud.patient";

    private Context context;
    private RemindTimeBean remindTimeBean;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == MessageCode.NETWORKCODE_GETREMINDTIMELIST_SUCCESS) {
                remindTimeBean = (RemindTimeBean) msg.obj;
                setRemindTimeTask task = new setRemindTimeTask();
                task.execute(remindTimeBean);
                //task.execute();
            }
        }
    };

    @Override
    public void onReceive(Context context, Intent intent) {
//        super.onReceive(context, intent);
        this.context = context;


        /**
         *
         系统消息 1
         评估消息 2
         监测消息 3
         课堂更新 4
         复诊日期 5--------暂无（和系统消息一样）
         医生评价 6--------
         正常监测数据 7
         */

        Log.i("---userStyle-->","Action:"+intent.getAction());

        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        if (application == null) {
            application = (YjhApplication) context.getApplicationContext();
        }

        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            if (!isAppOnAtTopTask(context)) {
                BadgeUtil.setBadgeCount(context, 1);
            }

            String str = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if (!TextUtils.isEmpty(str)) {
                System.out.println(str);
                try {
                    JSONObject jo = new JSONObject(str);
                    if (jo != null) {
                        String type = "";
                        type = jo.getString("type");
                        Log.i("---DDD--->","type1111:"+type);
                        if (!TextUtils.isEmpty(type)) {
                            if (isAppOnAtTopTask(context)) {
                                String userStyle = "";
                                LoginInfoDao loginInfoDao = new LoginInfoDao(context);
                                LoginInfo loginInfo = loginInfoDao.getUser();
                                if (loginInfo != null) {
                                    userStyle = loginInfo.getUserStyle();
                                }
                                if (type.equals("1")) {
                                    YjhApplication.msg_system = true;
                                    YjhApplication.msg_new = true;
                                    SharedPreferenceUtil.setSysMsg(context,"1");
                                } else if (type.equals("2")) {
                                    if (userStyle.equals(YjhApplication.userstyle_nurse)) {
                                        YjhApplication.msg_assessNurse = true;
                                        YjhApplication.msg_new = true;
                                    } else if (userStyle.equals(YjhApplication.userstyle_patient)) {
                                        YjhApplication.msg_assessPatient = true;
                                        YjhApplication.msg_new = true;
                                    } else if (userStyle.equals(YjhApplication.userstyle_doctor)) {
                                        YjhApplication.msg_assessNurse = true;
                                        YjhApplication.msg_new = true;
                                    }
                                } else if (type.equals("3")) {
                                    YjhApplication.msg_monitor = true;
                                    YjhApplication.msg_new = true;
                                } else if (type.equals("6")) {
                                    YjhApplication.msg_evalution = true;
                                    YjhApplication.msg_new = true;
                                } else if (type.equals("7")) {
                                    if (userStyle.equals(YjhApplication.userstyle_nurse)) {
                                        NurseFragmentActivity activity = NurseFragmentActivity.getInstance();
                                        if (activity != null) {
                                            activity.jpushRefreshPatients();
                                        }
                                    }
                                }
                                else if (type.equals(PUSH_TYPE_NEW_DOCTOR_REPLY))
                                {
                                    // [170515] hum add: 收到推送时实时刷新图文资讯
                                    ConsulteFragment consultFragment = ConsulteFragment.getInstance();
                                    if (consultFragment != null)
                                    {
                                        consultFragment.refresh();
                                    }
                                }

                                if (userStyle.equals(YjhApplication.userstyle_nurse)) {
                                    NurseFragmentActivity activity = NurseFragmentActivity.getInstance();
                                    if (activity != null) {
                                        activity.jpushNew(type);
                                    }
                                } else if (userStyle.equals(YjhApplication.userstyle_patient)) {
                                    UserFragmentActivity activity = UserFragmentActivity.getInstance();
                                    if (activity != null) {
                                        activity.jpushNew();
                                    }
                                } else if (userStyle.equals(YjhApplication.userstyle_doctor)) {
                                    DoctorFragmentActivity activity = DoctorFragmentActivity.getInstance();
                                    if (activity != null) {
                                        activity.jpushNew(false);
                                    }
                                }

                                if (getTopActivity(context).contains("MessageMainActivity")) {
                                    MessageMainActivity activity = MessageMainActivity.getInstance();
                                    if (activity != null) {
                                        activity.jpushNew();
                                    }
                                }
                            }
                        }
                    }
                } catch (JSONException e) {
                }
            }
            super.onReceive(context, intent);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            super.onReceive(context, intent);

            String str = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if (!TextUtils.isEmpty(str)) {
                String type = "";
                try {
                    JSONObject jo = new JSONObject(str);
                    if (jo != null) {
                        type = jo.getString("type");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (!TextUtils.isEmpty(type)) {
                    LoginInfoDao loginInfoDao = new LoginInfoDao(context);
                    LoginInfo loginInfo = loginInfoDao.getUser();
                    if (type.equals("8") && loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient)) {//刷新患者的提醒
                        Log.e("message", "type8");
//                        Toast.makeText(context, "收到了提醒单执行单的推送", Toast.LENGTH_LONG).show();
                        YiJiaHuNetWorkUtils.getRemindTimeList(handler);
                        if (isAppOnAtTopTask(context)) {
                            UserFragmentActivity activity = UserFragmentActivity.getInstance();
                            if (activity != null) {
                                activity.jpushRefreshRemind();
                            }
                        }
                    }
                }
            }
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            //跳转指定的Activity
            String str = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if (!TextUtils.isEmpty(str)) {
                System.out.println(str);
                try {
                    JSONObject jo = new JSONObject(str);
                    if (jo != null) {
                        String type = "";
                        type = jo.getString("type");
                        if (!TextUtils.isEmpty(type)) {
                            intentActivity(context, type);
                        }
                    }
                } catch (JSONException e) {
                }
            }
//            intentActivity
        }
    }

    //1系统提醒2评估提醒3监测提醒4课堂5收到复诊日期6患者端医生评价7录入正常监测数据8提醒/执行单修改
    private void intentActivity(Context context, String type) {
//        BadgeUtil.resetBadgeCount(context);
        Log.i("---userStyle-->","type:"+type);
        Log.i("---userStyle-->","isCurrentApp:"+isAppOnAtTopTask(context));
        boolean isCurrentApp = isAppOnAtTopTask(context);
        String topActivity = getTopActivity(context);

        String userStyle = "";
        LoginInfoDao loginInfoDao = new LoginInfoDao(context);
        LoginInfo loginInfo = loginInfoDao.getUser();
        if (loginInfo != null) {
            userStyle = loginInfo.getUserStyle();
        }

        if (type.equals("1") || type.equals("2") || type.equals("3") || type.equals("6")) {//msg
            if (isCurrentApp) {
//                if (type.equals("1")) {
//                    YjhApplication.msg_system = true;
//                    YjhApplication.msg_new = true;
//                } else if (type.equals("2")) {
//                    if (userStyle.equals(YjhApplication.userstyle_nurse)) {
//                        YjhApplication.msg_assessNurse = true;
//                        YjhApplication.msg_new = true;
//                    } else if (userStyle.equals(YjhApplication.userstyle_patient)) {
//                        YjhApplication.msg_assessPatient = true;
//                        YjhApplication.msg_new = true;
//                    }
//                } else if (type.equals("3")) {
//                    YjhApplication.msg_monitor = true;
//                    YjhApplication.msg_new = true;
//                }

//                if (userStyle.equals(YjhApplication.userstyle_nurse)) {
//                    NurseFragmentActivity activity = NurseFragmentActivity.getInstance();
//                    if (activity != null) {
//                        activity.jpushNew(type);
//                    }
//                } else if (userStyle.equals(YjhApplication.userstyle_patient)) {
//                    UserFragmentActivity activity = UserFragmentActivity.getInstance();
//                    if (activity != null) {
//                        activity.jpushNew();
//                    }
//                }
                Log.i("---userStyle-->","userStyle:"+userStyle);
                if (userStyle.equals(YjhApplication.userstyle_doctor))
                {
                    if (topActivity.contains("DoctorFragmentActivity"))
                    {
                        DoctorFragmentActivity activity = DoctorFragmentActivity.getInstance();
                        if (activity != null)
                        {
                            activity.jpushNew(true);
                        }
                    }
                    else
                    {
                        com.hxkcloud.patient.ActivityManager.getInstance().removeAllActivity();
                        Intent mIntent = new Intent();
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.DoctorFragmentActivity");
                        mIntent.putExtra("index", 1);
                        context.startActivity(mIntent);
                    }
                }
                else
                {
                    Log.i("---userStyle-->","boolean:"+topActivity.contains("MessageMainActivity"));
                    if (topActivity.contains("MessageMainActivity"))
                    {

                    } else
                    {
//                com.hxkcloud.patient.ActivityManager.getInstance().removeAllActivity();
                        Intent mIntent = new Intent();
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//                        mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.MessageMainActivity");
                        mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.MessageMainActivity");
//                    mIntent.putExtra("havenew", true);
                        context.startActivity(mIntent);
                    }
                }
            } else
            {
                if (!TextUtils.isEmpty(userStyle))
                {
                    if (userStyle.equals(YjhApplication.userstyle_nurse))
                    {
                        Intent mIntent = new Intent();
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.NurseFragmentActivity");
                        mIntent.putExtra("msg", true);
                        context.startActivity(mIntent);
                    } else if (userStyle.equals(YjhApplication.userstyle_patient))
                    {
                        Intent mIntent = new Intent();
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.UserFragmentActivity");
                        mIntent.putExtra("msg", true);
                        context.startActivity(mIntent);
                    } else if (userStyle.equals(YjhApplication.userstyle_doctor))
                    {
                        Intent mIntent = new Intent();
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.DoctorFragmentActivity");
                        mIntent.putExtra("index", 1);
                        context.startActivity(mIntent);
                    }
                }
            }
        } else if (type.equals("4")) {
            if (isCurrentApp) {
                if (topActivity.contains("NewsActivity")) {

                } else {
//                    com.hxkcloud.patient.ActivityManager.getInstance().removeAllActivity();
                    Intent mIntent = new Intent();
                    mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.NewsActivity");
                    mIntent.putExtra("type", YjhApplication.articlekind_course);
                    context.startActivity(mIntent);
                }
            } else {
                if (!TextUtils.isEmpty(userStyle)) {
                    if (userStyle.equals(YjhApplication.userstyle_nurse)) {
                        Intent mIntent = new Intent();
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.NurseFragmentActivity");
                        mIntent.putExtra("course", true);
                        context.startActivity(mIntent);
                    } else if (userStyle.equals(YjhApplication.userstyle_patient)) {
                        Intent mIntent = new Intent();
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.UserFragmentActivity");
                        mIntent.putExtra("course", true);
                        context.startActivity(mIntent);
                    } else if (userStyle.equals(YjhApplication.userstyle_doctor)) {
                        //理论上医生收不到新课堂提醒的
                        Intent mIntent = new Intent();
                        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.DoctorFragmentActivity");
//                        mIntent.putExtra("course", true);
                        context.startActivity(mIntent);
                    }
                }
            }
        } else if (type.equals("5")) {
            com.hxkcloud.patient.ActivityManager.getInstance().removeAllActivity();
            Intent mIntent = new Intent();
            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.UserFragmentActivity");
            mIntent.putExtra("page", 2);
            context.startActivity(mIntent);
        } else if (type.equals("7")) {
            if (isCurrentApp) {
                if (topActivity.contains("NurseFragmentActivity")) {
                    NurseFragmentActivity activity = NurseFragmentActivity.getInstance();
                    if (activity != null) {
                        activity.jpushRefreshPatients();
                    }
                } else {
                    com.hxkcloud.patient.ActivityManager.getInstance().removeAllActivity();
                    Intent mIntent = new Intent();
                    mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.NurseFragmentActivity");
                    mIntent.putExtra("index", 1);
                    context.startActivity(mIntent);
                }
            } else {
                Intent mIntent = new Intent();
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.NurseFragmentActivity");
                mIntent.putExtra("index", 1);
                context.startActivity(mIntent);
            }
        } else if (type.equals("10")){
//            Log.i("---userStyle-->","ento:"+"typeis10");
            if (!TextUtils.isEmpty(userStyle)){
                 if (userStyle.equals(YjhApplication.userstyle_patient)) {
                    Intent mIntent = new Intent();
                    mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    mIntent.setClassName("com.hxkcloud.patient", "com.hxkcloud.patient.activitys.PictureConsultActivity");
                    mIntent.putExtra("course", true);
                    context.startActivity(mIntent);
                }
            }
        } else {
            if (isCurrentApp) {

            } else {
                Intent mIntent = new Intent();
                mIntent.setClassName(packageName, "com.hxkcloud.patient.activitys.StartActivity");
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                context.startActivity(mIntent);
            }
        }
    }

    public static boolean isAppOnAtTopTask(Context mContext) {
        ActivityManager activityManager = (ActivityManager) mContext.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = mContext.getApplicationContext().getPackageName();

        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        if (appProcesses == null)
            return false;

        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }

        return false;
    }

    /**
     * 获取处于栈顶的Activity
     *
     * @param context
     */
    private String getTopActivity(Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

        if (runningTaskInfos != null) {
            return (runningTaskInfos.get(0).topActivity).toString();
        } else {
            return null;
        }

    }

    private class setRemindTimeTask extends AsyncTask<Object, Object, Boolean> {
        @Override
        protected Boolean doInBackground(Object... params) {
            Log.i("--DDD-->","222222222222");
            Alarms.clearAlarm(context);

            List<Long> times = new ArrayList<>();

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

            List<RemindTimeBean.rTime> rTimes = remindTimeBean.getRlist();
            List<RemindTimeBean.eTime> eTimes = remindTimeBean.getElist();
            List<RemindTimeBean.rTime> kTimes = remindTimeBean.getKlist();
            for (RemindTimeBean.rTime rTime : rTimes) {
                for (String t : rTime.getTimeList()) {
                    String time = rTime.getRemindDate() + " " + t;
                    Long timeLong = (long) 0;
                    try {
                        timeLong = format.parse(time).getTime();
                    } catch (ParseException e) {
                    }
                   times.add(timeLong);
                    // addTime(times, timeLong);
                }
            }
            for (RemindTimeBean.rTime rTime : kTimes) {
                for (String t : rTime.getTimeList()) {
                    String time = rTime.getRemindDate() + " " + t;
                    Long timeLong = (long) 0;
                    try {
                        timeLong = format.parse(time).getTime();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    times.add(timeLong);
                    // addTime(times, timeLong);
                }
            }
            Calendar c = Calendar.getInstance();
            Long now = c.getTime().getTime();
            for (RemindTimeBean.eTime eTime : eTimes) {
                for (String t : eTime.getTimeList()) {
                    String time = eTime.getEndDate() + " " + t;
                    Long timeLong = (long) 0;
                    try {
                        timeLong = format.parse(time).getTime();
                    } catch (ParseException e) {
                    }
                    for (long last = timeLong; last > now; last -= 24 * 60 * 60 * 1000) {
                        times.add(timeLong);
                        //addTime(times, last);
                    }
                }
            }
       /*     Log.e("集合-->",  System.currentTimeMillis()+":");
            Log.e("集合1-->",times.toString());*/
            Collections.sort(times);
            Log.i("集合-->",times.toString());
            for (long time : times) {
                if (time<System.currentTimeMillis())
                    continue;
                Date d = new Date(time);
                Calendar cTime = Calendar.getInstance();

                cTime.setTime(d);
                int hour = cTime.get(Calendar.HOUR_OF_DAY);
                int minute = cTime.get(Calendar.MINUTE);
                saveAlarm(time, time, hour, minute);
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if (aBoolean) {
//do noting
            }
        }
    }

    private void addTime(List<Long> times, long time) {
        if (!times.contains(time)) {
            if (times.size() == 0) {
                times.add(0, time);
                return;
            } else if (times.size() == 1) {
                if (times.get(0) > time) {
                    times.add(0, time);
                    return;
                } else {
                    times.add(1, time);
                    return;
                }
            } else {
                if (time < times.get(0)) {
                    times.add(0, time);
                    return;
                } else if (time > times.get(times.size() - 1)) {
                    times.add(times.size(), time);
                    return;
                }
                for (int i = 0; i < times.size() - 1; i++) {
                    long time1 = times.get(i);
                    long time2 = times.get(i + 1);
                    if (time > time1 && time < time2) {
                        times.add(i + 1, time);
                        return;
                    }
                }
            }
        }
    }

    private long saveAlarm(long date, long time, int mHour, int mMinutes) {
        Alarm alarm = new Alarm();
        alarm.enabled = true;
        alarm.hour = mHour;
        alarm.minutes = mMinutes;
        alarm.isDelay = false;
        alarm.date = date + "";
        alarm.time = time + "";

        long timee = 0;
        timee = Alarms.addAlarm(context, alarm);
        return timee;
    }

}
